﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DisplayBoardInfo.Server.Entities
{
    public class Patient : Entity
    {
        private Model.Patient patient;

        /// <summary>
        /// Creates new, empty entity.
        /// </summary>
        public Patient()
            : base()
        {
            this.patient = new Model.Patient();
        }

        /// <summary>
        /// Create specyfic instance of entity.
        /// </summary>
        /// <param name="PESEL">PESEL number of user</param>
        /// <exception cref="IndexOutOfRangeException">User with specyfic PESEL number was not found.</exception>
        /// <exception cref="ArgumentException">Invalid PESEL number.</exception>
        public Patient(int ID)
        {
                if (db.Patients.Where(i => i.ID == ID).Count() == 0) throw new IndexOutOfRangeException("Patient not found");
                this.patient = db.Patients.First(i => i.ID == ID);
        }

        /// <summary>
        /// Refresh all data in entity from database.
        /// </summary>
        public override void Refresh()
        {
            db.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, patient);
        }

        /// <summary>
        /// Saves changes in database.
        /// </summary>
        public override void Save()
        {
            db.SubmitChanges();
        }

        /// <summary>
        /// Gets ID of patient
        /// </summary>
        public int ID
        {
            get
            {
                return this.patient.ID;
            }
        }

        /// <summary>
        /// Gets or sets name of patient.
        /// </summary>
        public string PESEL
        {
            get
            {
                return this.patient.PESEL;
            }

            set
            {
                this.patient.PESEL = value;
            }
        }

        /// <summary>
        /// Gets or sets name of patient.
        /// </summary>
        public string Name
        {
            get 
            {
                return this.patient.Name;
            }

            set
            {
                this.patient.Name = value;
            }
        }

        /// <summary>
        /// Gets or stes last name of patient.
        /// </summary>
        public string LastName
        {
            get
            {
                return this.patient.LastName;
            }

            set
            {
                this.patient.LastName = value;
            }
        }
    }
}
